/*
-----------------------------------------------------------------------------
This source file is part of Labor3D
(Labor 3D Graphics Engine)

Copyright (c) 2009-2020 Rock Mao
* creator : Rock Mao
* created : 2009-5-2   22:29
* filename: LaborTexture.h
-----------------------------------------------------------------------------
*/
#ifndef __LABORTEXTURE_H__
#define __LABORTEXTURE_H__
#include "LaborCoreDef.h"
#include "LaborColor.h"
#include "LaborRenderSystemEnum.h"

NAMESPACE_LABOR3D_BEGIN



typedef struct 
{
	int m_Width;
	int m_Height;
	int m_BytePerPixel;
} sTexSize_t;

typedef unsigned int eLaborTexLoadArg;

typedef struct 
{
	eLPIXEL_FORMAT     m_Format;
	int                m_Picth;
	int                m_Width;
	int                m_Height;
	void*              m_Pixels;
} SURF_LOCK_INFO;



class LABORCORE_API ILaborTexture
{
protected:
	virtual ~ILaborTexture(){};
public:
	virtual void                getUVRange(float& minx , float& miny , float& maxx , float& maxy) = 0;

	virtual bool                validate() = 0;

	virtual bool                getDim(sTexSize_t& size) = 0;
	virtual eLPIXEL_FORMAT      getFormat() = 0;
	virtual void*               lock( SURF_LOCK_INFO & lockinfo) = 0;
	virtual void*               lockRect( SURF_LOCK_INFO & lockinfo , int x , int y , int w , int h) = 0;
	virtual void*               update(void* data  , bool directly = true) = 0;
	virtual void                unlock() = 0;

	virtual eLTexFilter          getFilter() = 0;
	virtual void                setFilter(eLTexFilter filter) = 0;
	virtual eLTexAddressMode     getAddressMode() = 0;
	virtual void                setAddressMode(eLTexAddressMode addMode ) = 0;


	virtual bool                load(const wchar_t* filename , eLaborTexLoadArg arg = eLoad_Normal) = 0;
	virtual bool                load(const wchar_t* filename,const char* buf , int len , eLaborTexLoadArg arg = eLoad_Normal) = 0;
	virtual bool                unload() = 0;
	virtual int                 getResSize() = 0;


	virtual bool                saveToFile(const char* file,eLPIXEL_FORMAT fmt = eL_PF_None) = 0;
	virtual int                 getHandle(int iLayer = 0) = 0;
	virtual bool                isFlipY() = 0;
	virtual void                setFlipY(bool bFlip) = 0;
	virtual ILaborTexture*      clone(bool bMultTex) = 0;

	virtual bool                grabRenderTargetData(int x , int y , int w , int h , void* data , int dataLen) = 0 ;
	//================================================================================
	virtual bool                create(int x , int y , eLPIXEL_FORMAT pixel_format ) = 0;
	virtual bool                destory() = 0 ; 
	virtual void*               toRenderTarget() = 0;
};


class LABORCORE_API CLTexture : public ILaborTexture
{
protected:
	eLTexFilter      m_Filter;
	eLTexAddressMode m_addMode;
	eLTextureType    m_TextureType;
	int             m_Width;
	int             m_Height;
	int             m_Depth;
	int             m_TexWidth;
	int             m_TexHeight;
	bool            m_bFlip;
	bool            m_bLoadCopy;
	bool            m_bLoadingFinish;
	float           m_texMemUsageScale ;
	bool            m_bFlipY;

protected:
	int m_refCount;

public:
	int          RefCount();
	int          AddRef() ;
	virtual int  Release();
	virtual int  KillObject() ;
public:

	//For file texture manager.
	static CLTexture*        newInstance(const char* resName);
	static void                deleteInstance(CLTexture* pTexture);
public:
	CLTexture();
	virtual bool                  onDeviceLost(bool bDeviceChange) {return true;}
	virtual bool                  getImageSize(int& w , int& h) = 0;
	virtual void                  viewPort2ImageCoord(float vx , float vy , float& ix , float& iy) = 0;
	virtual bool                  getPixel(int x , int y , CLColor_4f& _cl){return false;}
	eLTexFilter                    getFilter(){return m_Filter ;}
	void                          setFilter(eLTexFilter filter){m_Filter = filter; };
	eLTexAddressMode               getAddressMode(){return m_addMode;};
	void                          setAddressMode(eLTexAddressMode addMode ){m_addMode = addMode;} 


	virtual bool                  load(const wchar_t* filename , eLaborTexLoadArg arg = eLoad_Normal) = 0;
	virtual bool                  load(const wchar_t* filename,const char* buf , int len, eLaborTexLoadArg arg = eLoad_Normal) = 0;
	virtual int                   getResSize() = 0;
	virtual bool                  unload() = 0;
	virtual bool                  saveToFile(const char* file,eLPIXEL_FORMAT fmt = eL_PF_None) = 0;

	virtual int                   getHandle(int iLayer = 0) = 0;

	virtual bool                  isFlipY() = 0;
	virtual void                  setFlipY(bool bFlip) = 0;
	virtual ILaborTexture*        clone(bool bMultTex) = 0;
	/** Gets the type of texture 
	*/
	virtual eLTextureType getTextureType(void) const { return m_TextureType; }
protected:
	virtual ~CLTexture();
};
NAMESPACE_LABOR3D_END
#endif